﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class ErrorBiz
    {
        #region Properties

        private readonly IErrorDao _errorDao;

        #endregion

        #region Constructors

        public ErrorBiz()
        {
            _errorDao = new ErrorDao();
        }

        public ErrorBiz(IErrorDao errorDao)
        {
            _errorDao = errorDao;
        }

        #endregion

        #region Methods

        public Error GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _errorDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Error CreateError(Error error)
        {
            // validate inputs
            if (error == null) return null;
            if (string.IsNullOrEmpty(error.Code) || string.IsNullOrEmpty(error.Name)) return null;

            try
            {
                return _errorDao.AddError(error);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _errorDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _errorDao.GetCurrentIdentity() + 1;
        }

        public void UpdateError(Error error)
        {
            if (error == null) return;
            try
            {
                _errorDao.UpdateError(error);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetAllErrorsWs()
        {
            try
            {
                var peopleList = _errorDao.GetAllErrors();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateErrorWs(Error error)
        {
            // validate inputs
            if (error == null) return null;
            if (string.IsNullOrEmpty(error.Code) || string.IsNullOrEmpty(error.Name)) return null;

            try
            {
                var addedError = _errorDao.AddError(error);
                return new { Result = "OK", Record = addedError };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateErrorWs(Error error)
        {
            if (error == null) return null;
            try
            {
                _errorDao.UpdateError(error);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteErrorWs(int errorId)
        {
            try
            {
                _errorDao.DeleteError(errorId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers

        #endregion
    }
}
